package cn.wolfcode.service.impl;

import cn.wolfcode.common.exception.BusinessException;
import cn.wolfcode.common.web.CommonCodeMsg;
import cn.wolfcode.domain.AccountLog;
import cn.wolfcode.domain.AccountTransaction;
import cn.wolfcode.domain.OperateIntergralVo;
import cn.wolfcode.mapper.AccountLogMapper;
import cn.wolfcode.mapper.AccountTransactionMapper;
import cn.wolfcode.mapper.UsableIntegralMapper;
import cn.wolfcode.service.IUsableIntegralService;
import cn.wolfcode.web.msg.IntergralCodeMsg;
import com.alibaba.fastjson.JSON;
import io.seata.rm.tcc.api.BusinessActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;


@Service
public class UsableIntegralServiceImpl implements IUsableIntegralService {
    @Autowired
    private UsableIntegralMapper usableIntegralMapper;
    @Autowired
    private AccountTransactionMapper accountTransactionMapper;
    @Autowired
    private AccountLogMapper accountLogMapper;
    
    @Override
    public Boolean payOnIntergral(OperateIntergralVo intergralVo) {
       int m= usableIntegralMapper.descIntergral(intergralVo.getUserId(),intergralVo.getValue());
        return m>0;
    }
    
    
    
//    public Boolean refundOnintergral(OperateIntergralVo intergralVo) {
//        int m = usableIntegralMapper.addIntergral(intergralVo.getUserId(), intergralVo.getValue());
//        return m>0;
//    }
 
    @Override
    public Boolean refundTry( BusinessActionContext context,OperateIntergralVo operateIntergralVo) {
        System.out.println("try"+context);
        //检查事务日志是否有数据
        AccountTransaction accountTransaction = accountTransactionMapper.get(context.getXid(), String.valueOf(context.getBranchId()));
        
        if (accountTransaction==null){
            //如果没有数据，执行正常逻辑 插入事务日志
            AccountTransaction transaction = new AccountTransaction();
            transaction.setTxId(context.getXid());
            transaction.setActionId(String.valueOf(context.getBranchId()));
            transaction.setUserId(operateIntergralVo.getUserId());
            transaction.setGmtCreated(new Date());
            transaction.setGmtModified(new Date());
            transaction.setAmount(operateIntergralVo.getValue());
            transaction.setType("1");
            transaction.setState(AccountTransaction.STATE_TRY);
            accountTransactionMapper.insert(transaction);
            //退款 try不做增钱
        }else if (accountTransaction.getState()==AccountTransaction.STATE_TRY){
            //如果有数据，幂等 什么也不做
        }else if (accountTransaction.getState()==AccountTransaction.STATE_COMMIT){
            //如果有数据，并且数据是Commit 报异常
            throw new BusinessException(IntergralCodeMsg.OP_INTERGRAL_ERROR);
        }else if (accountTransaction.getState()==AccountTransaction.STATE_CANCEL){
            //如果有数据，并且数据是Cancel 空回滚
        }
        return true;
    }
   
    @Override
    public Boolean refundOnintergralCommit(BusinessActionContext context) {
      
            System.out.println("commit" + context);
            //检查事务日志是否有数据
            AccountTransaction accountTransaction = accountTransactionMapper.get(context.getXid(), String.valueOf(context.getBranchId()));
            if (accountTransaction == null) {
                //检查事务日志是否有数据，没有则抛异常
                throw new BusinessException(IntergralCodeMsg.OP_INTERGRAL_ERROR);
            } else if (accountTransaction.getState() == AccountTransaction.STATE_TRY) {
                //如果有数据，是try 正常逻辑
                //增加积分
                String voJSON = context.getActionContext("operateIntergralVo").toString();
                OperateIntergralVo vo = JSON.parseObject(voJSON, OperateIntergralVo.class);
                int m = usableIntegralMapper.addIntergral(vo.getUserId(), vo.getValue());
                //修改状态为Commit
              
                accountTransactionMapper.updateAccountTransactionState(
                        context.getXid(),
                        String.valueOf(context.getBranchId()),
                        AccountTransaction.STATE_COMMIT,//目标
                        AccountTransaction.STATE_TRY);//源
                
            } else if (accountTransaction.getState() == AccountTransaction.STATE_COMMIT) {
                //如果有数据，并且数据是Commit 幂等
            } else if (accountTransaction.getState() == AccountTransaction.STATE_CANCEL) {
                //如果有数据，并且数据是Cancel 抛异常
                throw new BusinessException(IntergralCodeMsg.OP_INTERGRAL_ERROR);
            }
        
        return true;
    }
    
    @Override
    public Boolean refundOnintergralCancel(BusinessActionContext context) {
        System.out.println("canel"+context);
        //检查事务日志是否有数据
        AccountTransaction accountTransaction = accountTransactionMapper.get(context.getXid(), String.valueOf(context.getBranchId()));
        String voJSON =  context.getActionContext("operateIntergralVo").toString();
        OperateIntergralVo vo= JSON.parseObject(voJSON,OperateIntergralVo.class);
        if (accountTransaction==null){
            //检查事务日志是否有数据，没有则插入一条事务日志 状态cancel
            //如果没有数据，执行正常逻辑 插入事务日志
            AccountTransaction transaction = new AccountTransaction();
            transaction.setTxId(context.getXid());
            transaction.setActionId(String.valueOf(context.getBranchId()));
            transaction.setUserId(vo.getUserId());
            transaction.setGmtCreated(new Date());
            transaction.setGmtModified(new Date());
            transaction.setAmount(vo.getValue());
            transaction.setType("1");
            transaction.setState(AccountTransaction.STATE_CANCEL);
            accountTransactionMapper.insert(transaction);
        }else if (accountTransaction.getState()==AccountTransaction.STATE_TRY){
            //如果有数据，是try 正常逻辑
            accountTransactionMapper.updateAccountTransactionState(
                    context.getXid(),
                    String.valueOf(context.getBranchId()),
                    AccountTransaction.STATE_CANCEL,//目标
                    AccountTransaction.STATE_TRY);//源
        }else if (accountTransaction.getState()==AccountTransaction.STATE_COMMIT){
            //如果有数据，并且数据是Commit 抛异常
            throw new BusinessException(IntergralCodeMsg.OP_INTERGRAL_ERROR);
        }else if (accountTransaction.getState()==AccountTransaction.STATE_CANCEL){
            //如果有数据，并且数据是Cancel 幂等 什么也不做
        }
        return false;
    }
}
